package com.cloudkinto.service.ecang.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.common.BeanConvert;
import com.cloudkinto.common.DicBusinessItemConstant;
import com.cloudkinto.common.exception.BizException;
import com.cloudkinto.common.utils.OkHttpUtils;
import com.cloudkinto.common.utils.StringsUtil;
import com.cloudkinto.common.utils.TimeUtils;
import com.cloudkinto.common.utils.XmlUtils;
import com.cloudkinto.dao.OrderEcangDao;
import com.cloudkinto.dao.OrderEcangProductDao;
import com.cloudkinto.entity.*;
import com.cloudkinto.service.ecang.ECangService;
import com.cloudkinto.service.ecang.vo.ECangPageProductRes;
import com.cloudkinto.service.ecang.vo.ECangPageRes;
import com.cloudkinto.service.ecang.vo.ECangResponse;
import com.cloudkinto.service.ecang.vo.EcangRequest;
import com.cloudkinto.service.orderoutput.OrderOutputPackageService;
import com.cloudkinto.service.orderoutput.OrderOutputProductService;
import com.cloudkinto.service.orderoutput.OrderOutputService;
import com.cloudkinto.service.orderoutput.vo.*;
import com.cloudkinto.service.product.ProductInfoService;
import com.cloudkinto.service.shop.ShopService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jdom2.JDOMException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)//事务回滚
public class ECangServiceImpl extends ServiceImpl<OrderEcangDao, OrderEcangDo> implements ECangService {
    //    private final String url = "http://ntzsn8b.eccang.com/default/svc-open/web-service-api";
//    private final String serviceCode = "KINTO_JP";
//    private final String serviceToken = "JDYECOPENAPI";
//    private final String userName = "15051142203";
//    private final String userPass = "EC15051142203";
    @Autowired
    private OrderOutputService orderOutputService;
    @Autowired
    private OrderOutputPackageService orderOutputPackageService;
    @Autowired
    private OrderOutputProductService orderOutputProductService;
    @Autowired
    private ProductInfoService productService;
    @Autowired
    private OrderEcangDao dao;
    @Autowired
    private OrderEcangProductDao orderEcangProductDao;
    @Autowired
    private ShopService shopService;


    @Override
    public void getStockPosition() {

    }

    @Override
    public void autoSyncShippingOrder() {
        LambdaQueryWrapper<ShopDo> shopDoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shopDoLambdaQueryWrapper.isNotNull(ShopDo::getEcUrl);
        List<ShopDo> shopDoList = shopService.getBaseMapper().selectList(shopDoLambdaQueryWrapper);
        for (ShopDo shopDo : shopDoList) {
            String startDate = TimeUtils.getCurrentDateFormat(-7);
            getPreShippingOrder(shopDo, 1, 500, 4, 1, "1", startDate, TimeUtils.getCurrentDateFormat(+1));
        }
    }

    @Override
    public void getPreShippingOrder(ShopDo shopDo, int page, int pageSize, int orderStatus, int achieveType) {
        getPreShippingOrder(shopDo, page, pageSize, orderStatus, achieveType, "", null, null);
    }

    @Override
    public void getPreShippingOrder(ShopDo shopDo, int page, int pageSize, int orderStatus) {
        getPreShippingOrder(shopDo, page, pageSize, orderStatus, 1, "", null, null);
    }

    @Override
    public void getPreShippingOrder(ShopDo shopDo, int page, int pageSize, int orderStatus, int achieveType, String dateType, String dateFor, String dateTo) {
        Map map = new HashMap();
        map.put("page", page);
        map.put("pageSize", pageSize);
        map.put("achieve_type", achieveType);
        map.put("dateType", dateType);
        map.put("dateFor", dateFor);
        map.put("dateTo", dateTo);
        String body = null;
        try {
            body = OkHttpUtils.ecangRequest(shopDo.getEcUrl(), buildXmlParam(shopDo, JSON.toJSONString(map), "getPreShippingOrder"));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JDOMException e) {
            e.printStackTrace();
        }
        log.debug(body);
        ECangResponse<List<ECangPageRes>> response = JSON.parseObject(body, ECangResponse.class);
        if (response.getCode().equals("200")) {
            if (response.getData() != null) {
                for (ECangPageRes eCangPageRes : JSON.parseArray(response.getData().toString(), ECangPageRes.class)) {
                    LambdaQueryWrapper<OrderEcangDo> wrapper = new LambdaQueryWrapper<>();
                    wrapper.eq(OrderEcangDo::getOrderCode, eCangPageRes.getOrderCode())
                            .eq(OrderEcangDo::getCompanyId, shopDo.getCompanyId())
                            .last("order by id desc limit 1");
                    OrderEcangDo orderEcangDo = dao.selectOne(wrapper);
                    boolean isNeedUpdate = false;
                    if (orderEcangDo == null) {
                        orderEcangDo = BeanConvert.INSTANCE.eCangPage2Order(eCangPageRes);
                        orderEcangDo.setShopId(shopDo.getId());
                        orderEcangDo.setCompanyId(shopDo.getCompanyId());
                        orderEcangDo.setCreateTime(new Date());
                        dao.insert(orderEcangDo);
                        for (ECangPageProductRes eCangPageProductRes : eCangPageRes.getProductList()) {
                            OrderEcangProductDo ecangProductDo = BeanConvert.INSTANCE.ecangProduct2Do(eCangPageProductRes);
                            ecangProductDo.setOrderId(orderEcangDo.getId());
                            orderEcangProductDao.insert(ecangProductDo);
                        }
                    } else {
                        int count = 0;
                        if (!stringCompare(orderEcangDo.getFirstname(), eCangPageRes.getReceiptAddress().getFirstname())) {
                            orderEcangDo.setFirstname(eCangPageRes.getReceiptAddress().getFirstname());
                            count++;
                        }
                        if (!stringCompare(orderEcangDo.getReferenceNo(), eCangPageRes.getReferenceNo())) {
                            orderEcangDo.setReferenceNo(eCangPageRes.getReferenceNo());
                            count++;
                        }
                        if (!stringCompare(orderEcangDo.getCompany(), eCangPageRes.getReceiptAddress().getCompany())) {
                            orderEcangDo.setCompany(eCangPageRes.getReceiptAddress().getCompany());
                            count++;
                        }
                        if (!stringCompare(orderEcangDo.getPostcode(), eCangPageRes.getReceiptAddress().getPostcode())) {
                            orderEcangDo.setPostcode(eCangPageRes.getReceiptAddress().getPostcode());
                            count++;
                        }
                        if (!stringCompare(orderEcangDo.getStreetAddress1(), eCangPageRes.getReceiptAddress().getStreetAddress1())) {
                            orderEcangDo.setStreetAddress1(eCangPageRes.getReceiptAddress().getStreetAddress1());
                            count++;
                        }
                        if (!stringCompare(orderEcangDo.getStreetAddress2(), eCangPageRes.getReceiptAddress().getStreetAddress2())) {
                            orderEcangDo.setStreetAddress2(eCangPageRes.getReceiptAddress().getStreetAddress2());
                            count++;
                        }
                        if (!stringCompare(orderEcangDo.getDistrict(), eCangPageRes.getReceiptAddress().getDistrict())) {
                            orderEcangDo.setDistrict(eCangPageRes.getReceiptAddress().getDistrict());
                            count++;
                        }
                        if (!stringCompare(orderEcangDo.getState(), eCangPageRes.getReceiptAddress().getState())) {
                            orderEcangDo.setState(eCangPageRes.getReceiptAddress().getState());
                            count++;
                        }
                        if (!stringCompare(orderEcangDo.getPhone(), eCangPageRes.getReceiptAddress().getPhone())) {
                            orderEcangDo.setPhone(eCangPageRes.getReceiptAddress().getPhone());
                            count++;
                        }
                        if (!stringCompare(orderEcangDo.getCountry(), eCangPageRes.getReceiptAddress().getCountry())) {
                            orderEcangDo.setCountry(eCangPageRes.getReceiptAddress().getCountry());
                            count++;
                        }
                        if (!stringCompare(orderEcangDo.getCity(), eCangPageRes.getReceiptAddress().getCity())) {
                            orderEcangDo.setCity(eCangPageRes.getReceiptAddress().getCity());
                            count++;
                        }
                        if (!stringCompare(orderEcangDo.getOrderStatus() + "", eCangPageRes.getOrderStatus())) {
                            orderEcangDo.setOrderStatus(Integer.parseInt(eCangPageRes.getOrderStatus()));
                            count++;
                        }
                        if (count > 0) {
                            dao.updateById(orderEcangDo);
                            isNeedUpdate = true;
                        }
                        LambdaQueryWrapper<OrderEcangProductDo> productDoLambdaQueryWrapper = new LambdaQueryWrapper<>();
                        productDoLambdaQueryWrapper.eq(OrderEcangProductDo::getOrderId, orderEcangDo.getId());
                        List<OrderEcangProductDo> orderEcangProductDos = orderEcangProductDao.selectList(productDoLambdaQueryWrapper);
                        int productCount = 0;
                        if (orderEcangProductDos.size() == eCangPageRes.getProductList().size()) {
                            for (ECangPageProductRes eCangPageProductRes : eCangPageRes.getProductList()) {
                                for (OrderEcangProductDo ecangProductDo : orderEcangProductDos) {
                                    if (ecangProductDo.getWarehouseSku().equals(eCangPageProductRes.getWarehouseProductBarcode()) && ecangProductDo.getQuantity() == eCangPageProductRes.getQuantity()) {
                                        productCount++;
                                    } else {

                                    }
//                                    OrderEcangProductDo ecangProductDo = BeanConvert.INSTANCE.ecangProduct2Do(eCangPageProductRes);
//                                    ecangProductDo.setOrderId(orderEcangDo.getId());
//                                    orderEcangProductDao.updateById(ecangProductDo);
                                }
                            }
                            if (productCount != orderEcangProductDos.size()) {
                                isNeedUpdate = true;
                                orderEcangProductDao.delete(productDoLambdaQueryWrapper);
                                for (ECangPageProductRes eCangPageProductRes : eCangPageRes.getProductList()) {
                                    OrderEcangProductDo ecangProductDo = BeanConvert.INSTANCE.ecangProduct2Do(eCangPageProductRes);
                                    ecangProductDo.setOrderId(orderEcangDo.getId());
                                    orderEcangProductDao.insert(ecangProductDo);
                                }
                            }
                        } else {
                            isNeedUpdate = true;
                            orderEcangProductDao.delete(productDoLambdaQueryWrapper);
                            for (ECangPageProductRes eCangPageProductRes : eCangPageRes.getProductList()) {
                                OrderEcangProductDo ecangProductDo = BeanConvert.INSTANCE.ecangProduct2Do(eCangPageProductRes);
                                ecangProductDo.setOrderId(orderEcangDo.getId());
                                orderEcangProductDao.insert(ecangProductDo);
                            }
                        }
                    }
                    if (!orderEcangDo.getCountry().equals("JP")) continue;
                    LambdaQueryWrapper<OrderOutputDo> orderOutputDoWrapper = new LambdaQueryWrapper<>();
                    orderOutputDoWrapper.eq(OrderOutputDo::getPlatformNumber, eCangPageRes.getOrderCode())
                            .eq(OrderOutputDo::getPlatform, "ecang").eq(OrderOutputDo::getCompanyId, shopDo.getCompanyId())
                            .last("order by id desc limit 1");
                    OrderOutputDo orderOutputDo = orderOutputService.getBaseMapper().selectOne(orderOutputDoWrapper);
                    if (orderOutputDo == null) {
                        if (eCangPageRes.getOrderStatus().equals("4") && (orderEcangDo.getIsSendOut() == null || orderEcangDo.getIsSendOut() == 0)) {
                            //已提交状态订单发送至筋斗云出库单
                            OrderOutputAddReq orderOutputAddReq = buildOrderOutput(shopDo, eCangPageRes);
                            String value = null;
                            try {
                                value = orderOutputService.add(orderOutputAddReq, 1L);
                            } catch (BizException e) {
                                e.printStackTrace();
                                value = StringsUtil.createI18Message(e.getMessage());
                            }

                            if (value != null) {
                                orderStatusModify(orderEcangDo.getShopId(), eCangPageRes.getOrderCode(), 2, null, null, value, null);
                                orderOutputDoWrapper = new LambdaQueryWrapper<>();
                                orderOutputDoWrapper.eq(OrderOutputDo::getPlatformNumber, eCangPageRes.getOrderCode());
                                orderOutputDoWrapper.eq(OrderOutputDo::getPlatform, "ecang")
                                        .eq(OrderOutputDo::getCompanyId, shopDo.getCompanyId())
                                        .last("order by id desc limit 1");
                                orderOutputDo = orderOutputService.getBaseMapper().selectOne(orderOutputDoWrapper);
                                if (orderOutputDo != null) {
                                    orderOutputDo.setPlatformNumber(eCangPageRes.getOrderCode());
                                    orderOutputDo.setPlatform("ecang");
                                    orderOutputService.updateById(orderOutputDo);
                                    updateEcangOderOutStatus(orderEcangDo);
                                }
                            } else {
                                updateEcangOderOutStatus(orderEcangDo);
                                log.info(String.format("已同步易仓单号：%s订单至OMS出库单", eCangPageRes.getOrderCode()));
                            }
                        }
                    } else if (eCangPageRes.getOrderStatus().equals("4")) {
                        //update
                        if (isNeedUpdate && orderOutputDo.getStatus() < DicBusinessItemConstant.outWaitSendOut) {
                            OrderOutputUpdateReq orderOutputUpdateReq = buildOrderOutputUpdateReq(shopDo, orderOutputDo, orderEcangDo);
                            orderOutputService.update(orderOutputUpdateReq, 1L);
                        }
                    } else if (eCangPageRes.getOrderStatus().equals("0")) {
                        //已取消状态订单进行截单
//                        if (orderOutputDo.getStatus() < DicBusinessItemConstant.outSendOut && orderOutputDo.getStatus() > DicBusinessItemConstant.orderCutOff) {
//
//                        } else if (orderOutputDo.getStatus() < DicBusinessItemConstant.outWaitSendOut) {
//                            orderOutputService.cutoff(orderOutputDo.getId(), 1L);
//                        } else {
                        orderOutputService.cutoff(orderOutputDo.getId(), 1L);
//                        }
                    }
                }
            }
        }
    }

    private void updateEcangOderOutStatus(OrderEcangDo orderEcangDo) {
        LambdaUpdateWrapper<OrderEcangDo> wrapper1 = new LambdaUpdateWrapper<>();
        wrapper1.set(OrderEcangDo::getIsSendOut, 1);
        wrapper1.set(OrderEcangDo::getIsUpload, 0);
        wrapper1.eq(OrderEcangDo::getOrderCode, orderEcangDo.getOrderCode());
        wrapper1.eq(OrderEcangDo::getCompanyId, orderEcangDo.getCompanyId());
        update(wrapper1);
    }

    private OrderOutputUpdateReq buildOrderOutputUpdateReq(ShopDo shopDo, OrderOutputDo orderOutputDo, OrderEcangDo orderEcangDo) {
        if (!stringCompare(orderOutputDo.getReceiverName(), orderEcangDo.getFirstname())) {
            orderOutputDo.setReceiverName(orderEcangDo.getFirstname());
        }
        if (!stringCompare(orderOutputDo.getOrderNo(), orderEcangDo.getReferenceNo())) {
            orderOutputDo.setOrderNo(orderEcangDo.getReferenceNo());
        }
        if (!stringCompare(orderOutputDo.getPostCode(), orderEcangDo.getPostcode())) {
            orderOutputDo.setPostCode(orderEcangDo.getPostcode());
        }
        if (!stringCompare(orderOutputDo.getTelPhone(), orderEcangDo.getPhone())) {
            orderOutputDo.setTelPhone(orderEcangDo.getPhone());
        }
        if (!stringCompare(orderOutputDo.getArea(), orderEcangDo.getState())) {
            orderOutputDo.setArea(orderEcangDo.getState());
        }
        OrderOutputUpdateReq req = BeanConvert.INSTANCE.orderOutPut2Update(orderOutputDo);
        req.setAddress(orderEcangDo.getCity() + orderEcangDo.getStreetAddress1() + orderEcangDo.getStreetAddress2());
        LambdaQueryWrapper<OrderOutputPackageDo> packageDoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        packageDoLambdaQueryWrapper.eq(OrderOutputPackageDo::getOrderOutputId, orderOutputDo.getId());
        List<OrderOutputPackageDo> packageDoList = orderOutputPackageService.getBaseMapper().selectList(packageDoLambdaQueryWrapper);
        List<OrderOutputPackageUpdateReq> packageAddReqs = packageDoList.stream().map(new Function<OrderOutputPackageDo, OrderOutputPackageUpdateReq>() {
            @Override
            public OrderOutputPackageUpdateReq apply(OrderOutputPackageDo orderOutputPackageDo) {
                OrderOutputPackageUpdateReq updateReq = BeanConvert.INSTANCE.orderPackage2Update(orderOutputPackageDo);
                return updateReq;
            }
        }).collect(Collectors.toList());

        LambdaQueryWrapper<OrderEcangProductDo> productDoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        productDoLambdaQueryWrapper.eq(OrderEcangProductDo::getOrderId, orderEcangDo.getId());
        List<OrderEcangProductDo> productDoList = orderEcangProductDao.selectList(productDoLambdaQueryWrapper);
        List<OrderOutputProductDetail> productDetailList = productDoList.stream().map(new Function<OrderEcangProductDo, OrderOutputProductDetail>() {
            @Override
            public OrderOutputProductDetail apply(OrderEcangProductDo orderOutputPackageDo) {
                OrderOutputProductDetail updateReq = BeanConvert.INSTANCE.ecangProduct2OrderUpdate(orderOutputPackageDo);
                updateReq.setOrderId(orderOutputDo.getId());
                updateReq.setOrderId(orderOutputDo.getId());
                updateReq.setCommoditySku(orderOutputPackageDo.getWarehouseSku());
                updateReq.setProductId(productService.getProductId(updateReq.getCommoditySku(), orderEcangDo.getCompanyId()));
                updateReq.setPackageId(packageAddReqs.get(0).getId());
                return updateReq;
            }
        }).collect(Collectors.toList());

        packageAddReqs.get(0).setProductList(productDetailList);
        req.setPackageList(packageAddReqs);
        req.setShopId(shopDo.getId());
        return req;
    }

    private boolean stringCompare(String city, String city1) {
        if (city == null && city1 == null) return true;
        if (city == null && city1 != null) return false;
        if (StringUtils.isBlank(city) && StringUtils.isBlank(city1)) return true;
        try {
            if (city.equals(city1)) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    private OrderOutputAddReq buildOrderOutput(ShopDo shopDo, ECangPageRes eCangPageRes) {
        OrderOutputAddReq req = BeanConvert.INSTANCE.eCangOrder2Out(eCangPageRes);
        req.setAddress(eCangPageRes.getReceiptAddress().getCity() + eCangPageRes.getReceiptAddress().getStreetAddress1() + eCangPageRes.getReceiptAddress().getStreetAddress2());
        List<OrderOutputPackageAddReq> packageAddReqs = new ArrayList<>();
        List<OrderOutputProductAddReq> productAddReqs = new ArrayList<>();
        for (ECangPageProductRes eCangPageProductRes : eCangPageRes.getProductList()) {
            OrderOutputProductAddReq productAddReq = new OrderOutputProductAddReq();
            try {
                Long productId = productService.getProductId(eCangPageProductRes.getWarehouseProductBarcode(), shopDo.getCompanyId());
                productAddReq.setProductId(productId);
            } catch (Exception e) {
                e.printStackTrace();
            }
            productAddReq.setCommoditySku(eCangPageProductRes.getWarehouseProductBarcode());
            productAddReq.setCount(eCangPageProductRes.getQuantity());
            productAddReqs.add(productAddReq);
        }
        OrderOutputPackageAddReq packageAddReq = new OrderOutputPackageAddReq();
        packageAddReq.setProductList(productAddReqs);
        packageAddReqs.add(packageAddReq);
        req.setSendDate(new Date());
        req.setOrderType(1);
        req.setShipmentType(0);
        req.setPackageList(packageAddReqs);
        req.setShopId(shopDo.getId());
        req.setCompanyId(shopDo.getCompanyId());
        req.setPlatform("ecang");
        return req;
    }

    private String buildXmlParam(ShopDo shopDo, String requsetParam, String serviceMethod) {
        EcangRequest requestParam = new EcangRequest(requsetParam, shopDo.getEcUsername(), shopDo.getEcUserpass(), shopDo.getEcCode(), shopDo.getEcToken(), serviceMethod);
        return XmlUtils.convertToXml(requestParam, "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" + "\txmlns:ns1=\"http://www.example.org/Ec/\">\n" + "\t<SOAP-ENV:Body>", "\t</SOAP-ENV:Body>\n" + "</SOAP-ENV:Envelope>");
    }

    @Override
    public boolean completeOrder(ShopDo shopDo, String orderCode, String smCode, String courierCode) {
        return completeOrder(shopDo, orderCode, smCode, courierCode, null, null, null, null);
    }

    @Override
    public boolean uploadTrackingNo(String orderCode, String orderNo, String trackingNumber) {
        return uploadTrackingNo(orderCode, orderNo, trackingNumber, null, null);
    }

    @Override
    public boolean completeOrder(ShopDo shopDo, String orderCode, String smCode, String courierCode, String length, String width, String height, String weight) {
        Map map = new HashMap();
        map.put("orderCode", orderCode);
        LambdaQueryWrapper<OrderEcangDo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderEcangDo::getOrderCode, orderCode);
        OrderEcangDo orderEcangDo = dao.selectOne(wrapper);
        if (!smCode.equals(orderEcangDo.getChannelCode())) {
        }
//        if (smCode != null) map.put("sm_code", smCode);
        if (courierCode != null) map.put("courierCode", courierCode);
        if (length != null) map.put("length", length);
        if (width != null) map.put("width", width);
        if (height != null) map.put("height", height);
        if (weight != null) map.put("weight", weight);
        String body = null;
        try {
            body = OkHttpUtils.ecangRequest(shopDo.getEcUrl(), buildXmlParam(shopDo, JSON.toJSONString(map), "completeOrder"));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JDOMException e) {
            e.printStackTrace();
        }
        log.info(body);
        ECangResponse<String> response = JSON.parseObject(body, ECangResponse.class);
        if (response.getCode().equals("200")) {
            return true;
        }
        return false;
    }

    @Override
    public boolean uploadTrackingNo(String orderCode, String orderNo, String trackingNumber, String smCode, String courierCode) {
        Map map = new HashMap();
        LambdaQueryWrapper<OrderEcangDo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderEcangDo::getOrderCode, orderCode).last("limit 1");
        OrderEcangDo orderEcangDo = dao.selectOne(wrapper);
        if (orderEcangDo == null) {
            //单号不存在，查平台单号
            wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(OrderEcangDo::getReferenceNo, orderNo)
                    .ne(OrderEcangDo::getOrderCode, orderCode)
                    .eq(OrderEcangDo::getOrderStatus, "4")
                    .last("limit 1");
            orderEcangDo = dao.selectOne(wrapper);
            if (orderEcangDo == null) return false;
        }
        ShopDo shopDo = shopService.getById(orderEcangDo.getShopId());
        if (shopDo == null) return false;
        if (orderEcangDo.getSmCode() != null && !orderEcangDo.getSmCode().equals(smCode)) {
            //如果渠道不一致，先调修改渠道方法
            boolean b = modifyTrackingNo(shopDo, orderCode, trackingNumber, smCode, null);
            if (b) return b;
        }
        map.put("orderCode", orderCode);
        if (trackingNumber != null) map.put("trackingNumber", trackingNumber);
        if (courierCode != null) map.put("courierCode", courierCode);
        String body = null;
        try {
            body = OkHttpUtils.ecangRequest(shopDo.getEcUrl(), buildXmlParam(shopDo, JSON.toJSONString(map), "uploadTrackingNo"));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JDOMException e) {
            e.printStackTrace();
        }
        log.info(body);
        ECangResponse<String> response = JSON.parseObject(body, ECangResponse.class);
        if (response.getCode().equals("200")) {

            return true;
        }
        return false;
    }

    @Override
    public boolean orderStatusModify(Long shopId, String orderCode, int status, String trackingNumber, Long trackId, String reason, String courierCode) {
        ShopDo shopDo = shopService.getById(shopId);
        if (shopDo == null) return false;
        Map map = new HashMap();
        map.put("orderCode", orderCode);
        map.put("status", status);
        if (StringUtils.isNotBlank(trackingNumber))
            map.put("trackingNumber", trackingNumber);
//        if (serviceCode != null) map.put("serviceCode", serviceCode);
        if (reason != null) map.put("reason", reason);
        if (courierCode != null) map.put("courierCode", courierCode);
        String body = null;
        try {
            body = OkHttpUtils.ecangRequest(shopDo.getEcUrl(), buildXmlParam(shopDo, JSON.toJSONString(map), "orderStatusModify"));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JDOMException e) {
            e.printStackTrace();
        }
        log.debug(body);
        ECangResponse<String> response = JSON.parseObject(body, ECangResponse.class);
        if (response.getCode().equals("200")) {
            return true;
        }
        return false;
    }
//
//    @Override
//    public String getOrderStatus(String orderCode) {
//        Map map = new HashMap();
//        map.put("orderCode", orderCode);
//        String body = null;
//        try {
//            body = OkHttpUtils.ecangRequest(url, buildXmlParam(shopDo, JSON.toJSONString(map), "getOrderStatus"));
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (JDOMException e) {
//            e.printStackTrace();
//        }
//        log.info(body);
//        ECangResponse<String> response = JSON.parseObject(body, ECangResponse.class);
//        if (response.getCode().equals("200")) {
//            return response.getData();
//        }
//        return response.getError();
//    }

    @Override
    public boolean modifyTrackingNo(ShopDo shopDo, String orderCode, String trackingNumber, String smCode, String courierCode) {
        Map map = new HashMap();
        map.put("orderCode", orderCode);
        if (trackingNumber != null) map.put("trackingNumber", trackingNumber);
        if (smCode != null) map.put("sm_code", smCode);
        if (courierCode != null) map.put("courierCode", courierCode);
        String body = null;
        try {
            body = OkHttpUtils.ecangRequest(shopDo.getEcUrl(), buildXmlParam(shopDo, JSON.toJSONString(map), "modifyTrackingNo"));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JDOMException e) {
            e.printStackTrace();
        }
        log.info(body);
        ECangResponse<String> response = JSON.parseObject(body, ECangResponse.class);
        if (response.getCode().equals("200")) {
            return true;
        }
        return false;
    }

    /**
     * @param shopId    shopId
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @return codeOrderList 仓库单号 对应 output.platformNumer
     */
    @Override
    public List<String> queryByShopIdAndNoUploadExpress(Long shopId, String startDate, String endDate) {
        List<String> codeOrderList = dao.queryByShopIdAndNoUploadExpress(shopId, startDate, endDate);
        if (codeOrderList == null) codeOrderList = new ArrayList<>();
        return codeOrderList;
    }
}
